package common.hua.com;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.print.PrintHelper;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hua.common.BaseActivity;
import com.hua.common.commonspdb.DPreference;
import com.hua.common.commontools.MyLog;
import com.hua.common.photopicker.view.PhotosListActivity;

import common.hua.com.http.HttptestAcitity;
import common.hua.com.myapplication.R;
import common.hua.com.mycontrolactivity.MyControlHomeActivity;
import common.hua.com.timeview.TimeViewActivity;
import common.hua.com.xlistview.XListViewActivity;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private Button bt = null;
    private Button controlBt = null;
    private Button xlistbt = null;
    private Button dbTest = null;

    private Button timeViewBt = null;
    private Button httpUtilBt = null;
    private Button printBt = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewItem(R.layout.mainlayout);
        this.initViews();
    }

    private void initViews() {
        this.bt = (Button) this.findViewById(R.id.button);
        this.bt.setOnClickListener(this);
        this.controlBt = (Button) this.findViewById(R.id.conbtn);
        this.controlBt.setOnClickListener(this);
        this.xlistbt = (Button) this.findViewById(R.id.xlist_test);
        this.xlistbt.setOnClickListener(this);
        this.dbTest = (Button) this.findViewById(R.id.spdbtest);
        this.dbTest.setOnClickListener(this);
        this.printBt = (Button) this.findViewById(R.id.printbt);
        this.printBt.setOnClickListener(this);

        this.timeViewBt = (Button) this.findViewById(R.id.timebt);
        this.timeViewBt.setOnClickListener(this);
        this.httpUtilBt = (Button) this.findViewById(R.id.httpbt);
        this.httpUtilBt.setOnClickListener(this);
        this.timeViewBt.setText(gettextString(1025));
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.button:
                intent.setClass(this, PhotosListActivity.class);
                intent.putExtra("isforresoult", false);
                startActivity(intent);
                break;
            case R.id.conbtn:
                intent.setClass(this, MyControlHomeActivity.class);
                startActivity(intent);
                break;

            case R.id.xlist_test:
                intent.setClass(this, XListViewActivity.class);
                startActivity(intent);
                break;
            case R.id.spdbtest:
                final DPreference dPreference = new DPreference(this, "dpref"); // this Preference comes for free from the library

                long start = System.currentTimeMillis();
                for (int i = 0; i < 1000; i++) {
                    dPreference.setPrefString("test_string", "test_pref");
                }
                long end = System.currentTimeMillis();
                MyLog.writeSystemLog(dPreference.getPrefString("test_string", "nidaye"));
                Toast.makeText(this, "" + (end - start), Toast.LENGTH_LONG).show();
                MyLog.writeSystemLog("DPreferecne called setString 1000 times cost : " + (end - start));
                break;

            case R.id.timebt:
                intent.setClass(this, TimeViewActivity.class);
                startActivity(intent);
                break;
            case R.id.httpbt:
                intent.setClass(this, HttptestAcitity.class);
                startActivity(intent);
                break;
            case R.id.printbt:
                doPhotoPrint();   //此功能只适用于5.0及以上系统
                break;
        }
    }


    private void doPhotoPrint() {
        PrintHelper photoPrinter = new PrintHelper(this);
        photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.app);
        photoPrinter.printBitmap("droids.jpg - test print", bitmap);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            bt.setText(data.getStringExtra("path"));
        }
    }


    public static SpannableString gettextString(int count) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        int num1 = count / 1024;
        int num2 = count % 1024;
        int first = 0;
        int second = 0;
        int third = 0;
        if (num1 > 0) {
            builder.append(num1 + "");
            first = builder.length();
            builder.setSpan(new AbsoluteSizeSpan(24), 0, first, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append("GB");
            second = builder.length();
            builder.setSpan(new AbsoluteSizeSpan(12), first, second, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (num2 > 0) {
            builder.append(num2 + "");
            third = builder.length();
            builder.setSpan(new AbsoluteSizeSpan(24), second, third, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append("MB");
            builder.setSpan(new AbsoluteSizeSpan(12), third, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        builder.setSpan(new ForegroundColorSpan(Color.parseColor("#0085cf")), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return new SpannableString(builder);
    }

}
